package com.llyb.lease.web.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.llyb.lease.model.entity.AttrKey;
import com.llyb.lease.model.entity.AttrValue;
import com.llyb.lease.model.entity.FeeValue;
import com.llyb.lease.web.admin.mapper.AttrKeyMapper;
import com.llyb.lease.web.admin.mapper.AttrValueMapper;
import com.llyb.lease.web.admin.mapper.FeeKeyMapper;
import com.llyb.lease.web.admin.service.AttrKeyService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.llyb.lease.web.admin.vo.attr.AttrKeyVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
* @author liubo
* @description 针对表【attr_key(房间基本属性表)】的数据库操作Service实现
* @createDate 2023-07-24 15:48:00
*/
@Service
public class AttrKeyServiceImpl extends ServiceImpl<AttrKeyMapper, AttrKey>
    implements AttrKeyService{

    @Autowired
    private AttrKeyMapper attrKeyMapper;

    @Autowired
    private AttrValueMapper attrValueMapper;

    @Override
    public List<AttrKeyVo>  customListAll() {
        QueryWrapper<AttrKey> wrapper = new QueryWrapper<>();
        wrapper.select("id","name");
        List<AttrKey> list = attrKeyMapper.selectList(wrapper);
        List<AttrValue> list1 = attrValueMapper.selectList(null);


        ArrayList<AttrKeyVo> resultVoList = new ArrayList<>();


        for (AttrKey attrKey : list) {

            AttrKeyVo attrKeyVo = new AttrKeyVo();
            ArrayList<AttrValue> list3 = new ArrayList<>();

            BeanUtils.copyProperties(attrKey,attrKeyVo);
            for (AttrValue attrValue : list1) {
                if(attrKey.getId().equals(attrValue.getAttrKeyId())){
                    list3.add(attrValue);
                }
            }
            attrKeyVo.setAttrValueList(list3);
            resultVoList.add(attrKeyVo);
        }

        return resultVoList;

        /*QueryWrapper<AttrKey> wrapper = new QueryWrapper<>();
        wrapper.select("id", "name");
        List<AttrKey> list = attrKeyMapper.selectList(wrapper);
        List<AttrValue> list1 = attrValueMapper.selectList(null);

        ArrayList<AttrKeyVo> resultVoList = new ArrayList<>();

        // 遍历 AttrKey 列表
        for (AttrKey attrKey : list) {
            // 为每次循环创建一个新的 AttrKeyVo 对象
            AttrKeyVo attrKeyVo = new AttrKeyVo();

            // 复制 AttrKey 属性到 AttrKeyVo
            BeanUtils.copyProperties(attrKey, attrKeyVo);

            // 创建存放 AttrValue 的列表
            ArrayList<AttrValue> list3 = new ArrayList<>();

            // 遍历 AttrValue 列表，筛选出对应的值
            for (AttrValue attrValue : list1) {
                if (attrKey.getId().equals(attrValue.getAttrKeyId())) {
                    list3.add(attrValue);
                }
            }

            // 设置 AttrKeyVo 中的 AttrValue 列表
            attrKeyVo.setAttrValueList(list3);

            // 将处理好的 AttrKeyVo 添加到结果列表
            resultVoList.add(attrKeyVo);
        }

        return resultVoList;*/
    }

    @Override
    public void customRemoveByAttrKeyId(Long attrKeyId) {
        LambdaQueryWrapper<AttrValue> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(AttrValue::getAttrKeyId,attrKeyId);
        attrValueMapper.delete(lambdaQueryWrapper);

        attrKeyMapper.deleteById(attrKeyId);
    }
}




